@extends('user.layouts.head')
@section('content')
    <body>
    <div id="app">
        <div class="auth">
            <div class="auth-container">
                <div class="card">
                    <header class="auth-header">
                        <h1 class="auth-title">找回密码</h1>
                    </header>
                    <div class="auth-content">
                        <div class="form-group">
                            <label>电子邮箱：</label>
                            <input type="email" class="form-control" id="email" placeholder="请输入您要找回的电子邮箱地址" required>
                            <!-- <small class="form-text text-danger">工作人员永远不会向您索要任何关于账户的信息。</small> -->
                        </div>
                        <div class="form-group">
                            <label>验证码：</label>
                            <div class="row">
                                <div class="col-7">
                                    <input type="text" class="form-control" id="captcha" maxlength="5" placeholder="请输入验证码" required>
                                </div>
                                <div class="col-4">
                                    <img src="{{captcha_src()}}" id="captcha_img" onclick="this.src='{{captcha_src()}}'+Math.random()">
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <button type="submit" class="btn btn-block btn-primary" onclick="resetLink()">发送链接</button>
                        </div>
                        <div class="form-group">
                            <p class="text-muted text-center">已有账户?
                                <a href="{{ route('login') }}">登录!</a>
                            </p>
                        </div>
                    </div>
                </div>

                <div class="text-center">
                    <a href="/" class="btn btn-secondary btn-sm">
                        <i class="fa fa-arrow-left"></i> 返回首页
                    </a>
                </div>

            </div>

        </div>
    </div>
    <script src="{{ mix('js/app.js') }}"></script>
    <script src="{{ asset('/js/main.js') }}"></script>
    <script>

        let isLoading = false;

        function resetLink() {
            if ( !$('#email').val() ) {
                toast('邮箱地址 不能为空。');
                return false
            }
            if ( !$('#captcha').val() ) {
                toast('验证码 不能为空。');
                return false
            }

            if ( isLoading === false) {
                isLoading = true;
                axios.post('email', {
                    email: $('#email').val(),
                    captcha: $('#captcha').val(),
                })
                    .then(function (response) {
                        isLoading = false;
                        toast(response.data.message);
                        $('#captcha_img').attr('src', '/captcha/default?' + '?' + Math.random());
                    })
                    .catch(function (error) {
                        isLoading = false;
                        $.each(error.response.data.errors, function (index, object) {
                            toast(object[0]);
                            $('#captcha_img').attr('src', '/captcha/default?' + '?' + Math.random());
                            return false;
                        });
                    })
            }
        }
    </script>
    </body>
    </html>
@endsection

